﻿using System;
using System.Collections.Generic; 
using Csu.Modsim.ModsimModel; 
using ASquared; 

namespace iwpm.optimization
{
    /// <summary>A class that prepares the MODSIM model to be suitable for the conditional gradient method for a given list of hydropower units.</summary>
    public class HydroPreparer
    {
        // Instance variables
        private Model _model; 
        private HydropowerUnit[] _units;
        private long _cost;

        // Constructors 
        public HydroPreparer(Model model, HydropowerUnit[] units, long initialcost)
        {
            _model = model;
            _units = units;
            _cost = initialcost; 
        }
        public HydroPreparer(Model model, HydropowerTarget[] targets, long initialcost)
        {
            _model = model;
            List<HydropowerUnit> list = new List<HydropowerUnit>(targets.Length);
            foreach (HydropowerTarget target in targets)
                foreach (HydropowerUnit unit in target.HydroUnits)
                    if (!list.Contains(unit))
                        list.Add(unit);
            _units = list.ToArray();
            _cost = initialcost;
        }

        /// <summary>Prepares the model to be solved by the conditional gradient method (sets link costs to 1).</summary>
        public void Prepare()
        {
            foreach (HydropowerUnit unit in _units)
                foreach (Link link in unit.FlowLinks)
                    if (link.mlInfo.cost == 0)
                        link.mlInfo.cost = _cost; 
        }
    }
}
